package awesome.request.processor;

import java.util.ArrayList;

import awesome.ServerConfig;
import awesome.request.RequestQueue;
import awesome.site.proprietor.SiteProprietor;

/**
 * Constructs multiple threads for the request processor to run on.
 * 
 * @author Chris
 *
 */
public class RequestDelegator {
	
	
	private ArrayList<Thread> requestProcessors;
	
	
	public RequestDelegator(RequestQueue _requestQueue, SiteProprietor _siteProprietor) {
		RequestProcessor requestProcessor = new RequestProcessor(_requestQueue, _siteProprietor);
		//initialize all processor threads
		int numWorkerThreads = 
				Integer.parseInt(ServerConfig.INSTANCE.getProperty("server.workerthreads"));
		requestProcessors = new ArrayList<Thread>(numWorkerThreads);
		for (int i=0;i<numWorkerThreads;i++)
			requestProcessors.add(new Thread(requestProcessor));
	}
	
	/**
	 * Starts all request processor threads.
	 */
	public void startDelegator() {
		for (Thread currThread:requestProcessors)
			currThread.start();
	}
	
	/**
	 * Stops (interrupts) all request processor threads.
	 */
	public void stopDelegator() {
		for (Thread currThread:requestProcessors)
			currThread.interrupt();
	}
}
